<?php
/**
 * @reference https://github.com/phpredis/phpredis
 * @description
 * @author    DAYU
 * @date 2015/11/26  14:54
 * @version    1.0
 * @modify
 */

class RedisDB
{
    protected static $_instance;
    protected $db;

    public function __construct($host, $pass, $port = 6379)
    {
        $this->connect($host, $pass, $port);
    }

    public static function getInstance($host, $pass, $port = 6379)
    {
        if (static::$_instance === null) {
            static::$_instance = new static($host, $pass, $port);
        }
        return static::$_instance;
    }

    public function connect($host, $pass, $port)
    {
        $this->db = new Redis();
        $this->db->connect($host, $port);
        $this->db->auth($pass);
    }

    /**
     * 如果key不存在，设置持久存在的key，存在覆盖原来的key值
     * For example,
     * $redis->set("name","qiqi");
     *
     * @param $key
     * @param $value
     */
    public function set($key, $value)
    {
        $this->db->set($key, $value);
    }

    /**
     * 如果key不存在，设置持久存在的key，存在则不做任何操作
     * For example,
     * $redis->setnx("name","qiqi");
     *
     * @param $key
     * @param $value
     */
    public function setnx($key, $value)
    {
        $this->db->setnx($key, $value);
    }

    /**
     * 设置有生存时间的key
     * For example,
     * $redis->setex("name","qiqi",3600);
     *
     * @param $key
     * @param $value
     * @param int $ttl
     */
    public function setex($key, $value, $ttl = 86400)
    {
        return $this->db->setex($key, $ttl, $value);
    }

    /**
     * 判断key是否存在
     * For example,
     * $redis->exists("qiqi")
     *
     * @param $key
     * @return boolean
     */
    public function exists($key)
    {
        return $this->db->exists($key);
    }

    /**
     * 返回 key 所关联的字符串值
     * for example,
     * $redis->get("qiqi")
     *
     * @param $key
     * @return mixed
     */
    public function get($key)
    {
        return $this->db->get($key);
    }

    /**
     * 删除指定key
     * For example,
     * $redis->delete("qiqi")
     * $redis->delete(["a","b"])
     *
     * @param str|array $key
     */
    public function delete($key)
    {
        $this->db->delete($key);
    }

    /**
     * 以秒为单位，返回给定 key 的剩余生存时间
     * 当 key 不存在时，返回 -2
     * 当 key 存在但没有设置剩余生存时间时，返回 -1
     * For example,
     * $redis->ttl("qiqi")
     *
     * @param $key
     */
    public function ttl($key)
    {
        return $this->db->ttl($key);
    }

    /**
     * value 值按从左到右的顺序依次插入到表头
     * For example,
     * $redis->setQueue("queue",1)
     *
     * @param $key
     * @param $value
     */
    public function setQueue($key, $value)
    {
        $this->db->lPush($key, $value);
    }

    /**
     * 获取队列数据
     * For example,
     * do{
     *    $task=$redis->getQueue($key);
     *    if($task[1]){
     *        echo $task[1];
     *    }
     * }while(true);
     *
     * @param $key
     * @param int $timeout
     * @return array
     */
    public function getQueue($key, $timeout = 30)
    {
        return $this->db->brPop($key, $timeout);
    }
}
